﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Configuration;

using SMG.Domain.Model.System;
using SMG.Domain.Model.Program;
using SMG.Domain.Model.Migration;
using Infrastructure.Repository.EntityFramework.System;
using Infrastructure.Repository.EntityFramework.Program;
using Infrastructure.Repository.EntityFramework.Migration;

using System.Data.Entity.Infrastructure;

namespace Infrastructure.Repository.EntityFramework
{
    public class SMGDbContext : DbContext
    {
        static Boolean IsUpdateDB = ConfigurationManager.AppSettings["IsUpdateDB"] == null ? false : Convert.ToBoolean(ConfigurationManager.AppSettings["IsUpdateDB"]);
        /// <summary>
        /// Entity Framework数据库初始化四种策略
        /// </summary>
        static SMGDbContext() {
            //【策略一】数据库不存在时重新创建数据库
            //Database.SetInitializer<SMGDbContext>(new CreateDatabaseIfNotExists<SMGDbContext>());

            //【策略二】每次启动应用程序时创建数据库
            //Database.SetInitializer<SMGDbContext>(new DropCreateDatabaseAlways<SMGDbContext>());

            //【策略三】模型更改时重新创建数据库
            //Database.SetInitializer<SMGDbContext>(new DropCreateDatabaseIfModelChanges<SMGDbContext>());

            //【策略四】从不创建数据库
            //Database.SetInitializer<SMGDbContext>(null);

            if (IsUpdateDB)
                Database.SetInitializer<SMGDbContext>(new DropCreateDatabaseIfModelChanges<SMGDbContext>());
            else
                Database.SetInitializer<SMGDbContext>(null);
        }

        public SMGDbContext()
            : base("SMG")
        { }

        #region System Properties

        //public DbSet<User> User {
        //    get { return Set<User>(); }
        //}
        public DbSet<ProgramBill> ProgramBill {
            get { return Set<ProgramBill>(); }
        }

        public DbSet<BillVersionRecord> BillVersionRecord {
            get { return Set<BillVersionRecord>(); }
        }
        public DbSet<Channel> Channel {
            get { return Set<Channel>(); }
        }
        public DbSet<Device> Device
        {
            get { return Set<Device>(); }
        }

        public DbSet<SysDict> SysDict {
            get { return Set<SysDict>(); }
        }

        public DbSet<PlayRecord> PlayRecord
        {
            get { return Set<PlayRecord>(); }
        }

        public DbSet<FirstMoveTask> FirstMoveTask
        {
            get { return Set<FirstMoveTask>(); }
        }
        public DbSet<FirstMoveTaskDetail> FirstMoveTaskDetail
        {
            get { return Set<FirstMoveTaskDetail>(); }
        }
        public DbSet<FirstMoveTaskState> FirstMoveTaskState
        {
            get { return Set<FirstMoveTaskState>(); }
        }
        public DbSet<FirstMoveTaskDetailState> FirstMoveTaskDetailState
        {
            get { return Set<FirstMoveTaskDetailState>(); }
        }
        public DbSet<SecondMoveTask> SecondMoveTask
        {
            get { return Set<SecondMoveTask>(); }
        }
        public DbSet<SecondMoveTaskDetail> SecondMoveTaskDetail
        {
            get { return Set<SecondMoveTaskDetail>(); }
        }

        #endregion

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations
                .Add(new ProgramBillConfiguration())
                .Add(new SysDictTypeConfiguration())
                .Add(new PlayRecordConfiguration())
                .Add(new FirstTaskConfiguration())
                .Add(new FirstTaskDetailConfiguration())
                .Add(new FirstTaskStateConfiguration())
                .Add(new FirstTaskDetailStateConfiguration())
                .Add(new SecondTaskConfiguration())
                .Add(new SecondTaskDetailConfiguration())
                .Add(new ChannelConfiguration())
                .Add(new DeviceConfiguration());

            base.OnModelCreating(modelBuilder);
        }        
    }
}
